package com.zb.session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * HttpServletRequest包装类
 * @version
 * @since JDK 1.7
 */
public class UmeiHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private HttpSession session;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private String sid;

    public UmeiHttpServletRequestWrapper(String sid, HttpServletRequest request) {
        super(request);
        this.sid = sid;
    }

    public UmeiHttpServletRequestWrapper(String sid,
            HttpServletRequest request, HttpServletResponse response) {
        super(request);
        this.sid = sid;
        this.request = request;
        this.response = response;
        if (session == null) {
            session = new HttpSessionWrapper(this.sid, super.getSession(false),
                    request, response);
        }
    }

    public HttpSession getSession(boolean create) {
        if (session == null) {
            if (create) {
                this.session = new HttpSessionWrapper(this.sid,
                        super.getSession(create), request, response);
                return session;
            } else {
                return null;
            }
        } else {
            return session;
        }
    }

    public HttpSession getSession() {
        if (session == null) {
            this.session = new HttpSessionWrapper(this.sid, super.getSession(),
                    request, response);
            return session;
        }
        return session;
    }
}
